package com.bnz.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bnz.common.PageUtils;
import com.bnz.common.entity.R;

import com.bnz.shop.entity.SpecificationOptionEntity;
import com.bnz.shop.entity.TypeTemplateEntity;
import com.bnz.shop.service.SpecificationOptionService;
import com.bnz.shop.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.bnz.shop.entity.SpecificationEntity;
import com.bnz.shop.service.SpecificationService;





/**
 * 
 *
 * @author hbxfwf
 * @email 2568783935@qq.com
 * @date 2023-02-16 09:57:43
 */
@RestController
@RequestMapping("shop/specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;
    @Autowired
    private SpecificationOptionService optionService;
    @Autowired
    private TypeTemplateService typeTemplateService;

    /**
     * 列表
     */
    @GetMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
        PageUtils page = specificationService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("/findOptionsBySpecId")
    public R findOptionsBySpecId(@RequestBody List<Map> list){
        for (Map map : list) {
            //1. 得到规格id
            String id = map.get("id").toString();
            //2. 根据规格id，查询规格选项
            List<SpecificationOptionEntity> options = optionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", id));
            //3. 为map添加属性
            map.put("options",options);
        }
        return R.ok().put("specs",list);
    }
    /**
     * 信息
     */
    @GetMapping("/info/{id}")
        public R info(@PathVariable("id") String id){
		SpecificationEntity specification = specificationService.getById(id);

        return R.ok().put("specification", specification);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
        public R save(@RequestBody SpecificationEntity specification){
		specificationService.save(specification);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
        public R update(@RequestBody SpecificationEntity specification){
		specificationService.updateById(specification);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
        public R delete(@RequestBody String[] ids){
		specificationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    // 根据模板id的值查出规格及规格选项
    @GetMapping("/findSpecAndOptions/{typeId}")
    public R findSpecAndOptions(@PathVariable String typeId){
        //1. 根据模板id得到模板对象
        TypeTemplateEntity template = typeTemplateService.getById(typeId);
        //2. 得到此模板，此模板下的所有的规格列表
        String specIds = template.getSpecIds();
        //3. 转换为List<Map>格式
        List<Map> maps = JSON.parseArray(specIds, Map.class);
        //4. 遍历上面的规格集合，为每个规格添加规格选项列表
        for (Map map : maps) {
            //4.1 得到规格id
            String id = map.get("id") + "";
            //4.2 根据规格id查询出规格选项列表
            List<SpecificationOptionEntity> options = optionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", id));
            //4.3 为map添加一个optionskey
            map.put("options",options);
        }
        //5. 返回
        return R.ok().put("specList",maps);
    }
}
